<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增轮播图</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
</head>
<style>
    .layui-form-label{
        width: 60px !important;
    }
</style>
<body style="height: 750px;background-color: white">
<div style="width:80%;height:100%;margin:15px auto;overflow:auto;">
    <form class="layui-form" action="">
        <div class="layui-form-item" id="title_div">
            <label class="layui-form-label" >标题</label>
            <div class="layui-input-block">
                <input type="text" name="title"  placeholder="请输入标题" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" id="message_div">
            <label class="layui-form-label" >描述</label>
            <div class="layui-input-block">
                <input type="text" name="message"  placeholder="请输入描述" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <input type="radio" lay-filter="type" name="type" value="0" title="普通图片" checked=""><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i><span >普通图片</span></div>
                <input type="radio" lay-filter="type" name="type" value="1" title="超链接"><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i><span >超链接</span></div>
            </div>
        </div>

        <div class="layui-form-item" id="aa" style="display: none">
            <label class="layui-form-label" >超链接</label>
            <div class="layui-input-block">
                <input type="text" name="url" id="url"  placeholder="请输入超链接网址" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">顺序权重</label>
            <div class="layui-input-block">
                <input name="sort" type="number" min="0" max="999999999" placeholder="顺序权重，只能输入正整数" id="sort" required lay-verify="sort" placeholder="商品权重数字" autocomplete="off" class="layui-input" onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')">
            </div>
        </div>
        <!-- 图片上传 -->
        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test" style="display: inline;float: left;" >上传图片</button>
            <div class="layui-upload-list" style="display: inline;margin-left: 20px;">
                <img class="layui-upload-img" name="logo" id="logo" style="width: 360px;height: 200px">
                <p id="demoText"></p>
            </div>
            <span style="font-size: 14px;color: red;margin-left: 110px" >建议图片的高宽比为 1 : 1.8，例如200*360 </span>
            <input type="hidden" class="input-text" id="picture" name="picture">
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" >状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="启用" checked=""><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i><span >启用</span></div>
                <input type="radio" name="status" value="0" title="禁用"><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i><span >禁用</span></div>
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button class="layui-btn" id="addWuye" lay-submit lay-filter="addWuye" >立即添加</button>
                <button type="reset" id="reset" class="layui-btn layui-btn-primary" >重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/layui/layui.js"></script>
<script src="/js/layui/layui.all.js"></script>
<script src="/admin/js/token.js"></script>

<script>
    var $ = layui.jquery;

    layui.use(['form' , 'jquery' , 'upload'], function(){
        var form = layui.form;

        var upload = layui.upload;

        var type;//轮播图类型（0：普通  1：链接）
        //监听多选框点击事件  主要是通过 lay-filter="touzi"  来监听
        form.on('radio(type)', function (data) {
            // console.log( data );　　   //打印当前选择的信息
            var value = data.value;
            type = value;
            if(value==1){//判断当前多选框是选中还是取消选中
                $("#aa").css("display","block");
            }else{
                $("#aa").css("display","none");
                $("#url").val("");
            }
        });

        //添加操作
        form.on('submit(addWuye)',function(data){
            var bg = $("input[name='picture']").val();
            var sort = $("input[name='sort']").val();
            var title = $("input[name='title']").val();
            var message = $("input[name='message']").val();
            var type = $("input[name='type']:checked").val();
            var url = $("#url").val();
            if (type == 1){
                if (url == "" || url ==null){
                    layer.msg("请填写超链接");
                    return false;
                }
            }
            if(bg==null || bg==""){
                layer.msg('请上传图片！');
            }else if (title=='' || title==null){
                layer.msg("请填写标题");
            }else if (message=='' || message==null){
                layer.msg("请填写描述");
            }else if(sort=='' || sort==null){
                layer.msg("请输入顺序权重");
            }else{
                data.field.picture=bg;
                var msgMovie = layer.load(1);
                $.ajax({
                    url:"/lunbotu/addLunbotu"
                    ,type:"POST"
                    ,contentType: 'application/json'
                    ,data:JSON.stringify(data.field)
                    ,success:function (data) {
                        if(data.code==200){
                            layer.close(layer.index);
                            layer.msg('添加成功');
                            setTimeout(function(){
                                window.location.reload();
                            },1000);
                        }else {
                            layer.msg(data.msg);
                        }
                    }
                    ,error:function () {
                        console.log("ajax error");
                    }
                });
                layer.close(msgMovie);
            }
            return false;
        });

        //bg上传
        var uploadInst = upload.render({
            elem: '#test'
            , url: '/admin/upload/uploadimg' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#logo').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //判断上传是否成功
                if (res.code == 0) {
                    $("#picture").val(res.data);
                    return layer.msg('上传成功！');
                }else{
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
        // 设置下拉列表的高度
        $(".layui-form-select dl").css("max-height",'400px');
    });
</script>

</body>
</html>